#include "Anx_spwm.h"

#if defined(HAL_TIM_MODULE_ENABLED)
#include "tim.h"

/* Private includes ----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private typedef ------------------------------------------------------------*/

/* Extern variables ---------------------------------------------------------*/


/* Private variables ---------------------------------------------------------*/
// static const char TAG[] = "SPWM";
__IO uint16_t spwm_array[] = {
224, 0, 112,	224, 0, 112,	224, 0, 112,	224, 0, 112,	224, 0, 112,	
224, 0, 112,	224, 0, 112,	224, 0, 112,	224, 0, 112,	224, 0, 112,	
224, 0, 112,	224, 0, 112,	224, 0, 112,	224, 0, 112,	224, 0, 112,	
224, 0, 112,	224, 0, 112,	224, 0, 112,	224, 0, 112,	224, 0, 112,	
};


/* Public variables ---------------------------------------------------------*/
Anx_SPWM_ConfigTypeDef spwmConfig={
    .htim=&htim1,
    .Channel=TIM_CHANNEL_1,
    .BurstBaseAddress=TIM_DMABASE_ARR,
    .BurstRequestSrc=TIM_DMA_CC1,
    .BurstBuffer=(uint32_t *)spwm_array,
    .BurstLength=TIM_DMABurstLength_3Transfers,
    .DataLength=60
};


/* Private callback ---------------------------------------------------------*/
//系统回调函数
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef* htim)
{
    if (htim->Instance == TIM1)
    {
        /**********   关闭定时器与spwm   **********/
        // TIM1->CCER &= 0b11111010;
        spwm_stop(&spwmConfig);

        // HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9, GPIO_PIN_RESET); //LED亮 //快速拉低pwm输出引脚

        /**********   后续操作   **********/
        // HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&g_ad_data_buf, g_original_g_SampleNum);
    }
}

/* Private functions ---------------------------------------------------------*/

void Anx_TIM_DMABurst_MultiWriteStart(Anx_SPWM_ConfigTypeDef* Config)
{
    HAL_TIM_DMABurst_MultiWriteStart(Config->htim,
        Config->BurstBaseAddress,
        Config->BurstRequestSrc,
        Config->BurstBuffer,
        Config->BurstLength,
        Config->DataLength);

    HAL_TIM_PWM_Start(Config->htim, Config->Channel);
}

void Anx_TIM_DMABurst_MultiWriteStop(Anx_SPWM_ConfigTypeDef* Config)
{
    HAL_TIM_DMABurst_WriteStop(Config->htim,Config->BurstRequestSrc);

    HAL_TIM_PWM_Stop(Config->htim, Config->Channel);
}


#endif